package com.novax.ex.activity.provider.handler.provider;

import com.novax.ex.common.constant.MqConstants;
import com.novax.ex.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;

import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;
import java.util.Date;

/**
 * @Description: IEO打新活动生产者
 * @Author: ChenXi
 * @Date:  2022/12/13 17:25
 * @Param:
 * @return:
 **/
@Slf4j
@Component
public class IeoProviderService {

    @Resource
    private AmqpTemplate amqpTemplate;

    /**
     * @Description: 活动结束, 发送计算发币量MQ消息
     * @Author: ChenXi
     * @Date:  2022/12/14 09:36
     * @Param: [userId, unlockTime]
     * @return: void
     **/
    public void sendIeoCalculi(String ieoId, Date endTime) {
        // 精确到分的活动结束时间 (延后2分钟)
        Date discardSecondEndTime = DateUtil.getNextMinute(DateUtil.discardSecond(endTime),2);
        Long endCountDown = discardSecondEndTime.getTime() - System.currentTimeMillis();
        log.info("mq provider ieoId:{}, endTime:{}, discardSecondEndTime:{}, endCountDown:{}",ieoId, endTime, discardSecondEndTime, endCountDown);
        amqpTemplate.convertAndSend(MqConstants.Exchange.ACTIVITY_IEO_SETTLE_EXCHANGE
            , MqConstants.Exchange.ACTIVITY_IEO_SETTLE_KEY
            , ieoId
            , correlationData -> {
                correlationData.getMessageProperties().setDelayLong(endCountDown);
                return correlationData;
            });
    }

//    /**
//     * @Description: 发送活动解冻倒计时MQ消息
//     * @Author: ChenXi
//     * @Date:  2022/12/14 09:36
//     * @Param: [userId, unlockTime]
//     * @return: void
//     **/
//    public void sendIeoIdUnfree(String ieoId, Date unlockTime) {
//        // 精确到分的解锁时间
//        Date discardSecondUnlockTime = DateUtil.discardSecond(unlockTime);
//        Long unfreeCountDown = discardSecondUnlockTime.getTime() - System.currentTimeMillis();
//        log.info("mq provider ieoId:{}, unlockTime:{}, unfreeCountDown:{}",ieoId, unlockTime, unfreeCountDown);
//            amqpTemplate.convertAndSend(MqConstants.Exchange.ACTIVITY_IEO_UNFREE_EXCHANGE
//                    , MqConstants.Exchange.ACTIVITY_IEO_UNFREE_KEY
//                    , ieoId
//                    , correlationData -> {
//                        correlationData.getMessageProperties().setDelay(unfreeCountDown.intValue());
//                        return correlationData;
//                    });
//    }


}
